//
//  WXLCollectionReusableView.m
//  段子手
//
//  Created by dllo on 16/3/19.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "WXLCollectionReusableView.h"

@interface WXLCollectionReusableView ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) FenglyNuoScrollView *scrollView;
@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) NSMutableArray *imgUrlArr;
@property (nonatomic, retain) NSArray *fourArr;
//@property (nonatomic, retain) UIView *lastView;

@end


@implementation WXLCollectionReusableView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.imgUrlArr = [NSMutableArray new];
        
        [self createCollectionView];
        
//        [self createLastView];
    }
    return self;
}

- (void)setModel:(WXLDataModel *)model {
    _model = model;
    
    NSArray *arr = _model.dataArr;
    
    for (WXLActionModel *model in arr) {
        [_imgUrlArr addObject:model.picUrl];
    }
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        [self createSubView];
    });
}

- (void)setFourModel:(WXLDataModel *)fourModel {
    _fourModel = fourModel;
    
    self.fourArr = _fourModel.dataArr;
    
    [_collectionView reloadData];
}



#pragma mark - 创建子视图
// 创建子视图
- (void)createSubView {
    [self createScrollView];
}

///** 创建标题类 */
//- (void)createLastView {
////    self.lastView = [UIView alloc] initWithFrame:CGRectMake(10, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)
//}

/** 创建collectionView */
- (void)createCollectionView {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 60 / 2;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 0, 10);
    flowLayout.itemSize = CGSizeMake((kUIScreenWidth - 30 * 3 - 20) / 4, 70);
    
    if (kUIScreenHeight >= 568) {
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - 70, kUIScreenWidth, 70) collectionViewLayout:flowLayout];
        // 如果是iPhone4s时
    } else {
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - 60, kUIScreenWidth, 70) collectionViewLayout:flowLayout];
    }
    
    [self addSubview:_collectionView];
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor clearColor];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    // 注册cell重用池
    [_collectionView registerClass:[WXLReusableCollectionViewCell class] forCellWithReuseIdentifier:@"WXLReusableCollectionViewCell"];
}

- (void)createScrollView {
    self.scrollView = [FenglyNuoScrollView fenglyNuoScrollViewWithImageUrlArray:_imgUrlArr Frame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height - 70)];
    [self addSubview:_scrollView];
    _scrollView.time = 4;
}



#pragma mark - collectionView Delegate & DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _fourArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WXLReusableCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WXLReusableCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    
    cell.model = _fourArr[indexPath.row];
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    // 调用代理方法 实现跳转
    [self.delegate cellClick:indexPath];
}




@end
